package com.yougong.api.modules.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yougong.api.modules.customer.model.vo.FileVO;
import com.yougong.api.util.AliossUtils;
import com.yougong.api.util.FileUtils;
import com.yougong.api.util.OssContants;
import com.yougong.common.api.CommonResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.UUID;

/**
 * @author: muchen
 * @createDate: 2021年3月4日
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
@SaCheckPermission("common")
public class CommonController {

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/file")
    public CommonResult uploadFile(MultipartFile file, HttpServletRequest request) {
        String bucketName = OssContants.DEFAULT_BUCKET_NAME;
        String uploadDomain = OssContants.DEFAULT_DOMAIN;
        String path = OssContants.PATH;
        FileVO fileVO = new FileVO();
        try {
            fileVO.setFileName(FileUtils.getFileName(file));
            fileVO.setFileSize(file.getSize());
            String fileFormat = FileUtils.getFileType(file);
            fileVO.setFileFormat(fileFormat);
            String fileUrl = AliossUtils.uploadfilesByName(uploadDomain, bucketName, path, file,
                    file.getOriginalFilename());
            fileVO.setFileUrl(fileUrl);
        }catch (Exception e){
            log.error("文件为空，文件上传失败");
            return CommonResult.failed();
        }

        return CommonResult.success(fileVO);
    }

}
